package cn.iocoder.yudao.module.wine.service.supplytype;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import cn.iocoder.yudao.module.wine.controller.app.supplytype.enums.SupplyTypeEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.wine.controller.admin.supplytype.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplytype.SupplyTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.supplytype.SupplyTypeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 酒类类型 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SupplyTypeServiceImpl implements SupplyTypeService {

    @Resource
    private SupplyTypeMapper supplyTypeMapper;


    @Resource
    private DictDataApi dataApi;

    @Override
    public Integer createSupplyType(SupplyTypeSaveReqVO createReqVO) {
        // 插入
        SupplyTypeDO supplyType = BeanUtils.toBean(createReqVO, SupplyTypeDO.class);
        supplyTypeMapper.insert(supplyType);
        // 返回
        return supplyType.getId();
    }

    @Override
    public void updateSupplyType(SupplyTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplyTypeExists(updateReqVO.getId());
        // 更新
        SupplyTypeDO updateObj = BeanUtils.toBean(updateReqVO, SupplyTypeDO.class);
        supplyTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplyType(Integer id) {
        // 校验存在
        validateSupplyTypeExists(id);
        // 删除
        supplyTypeMapper.deleteById(id);
    }

    private void validateSupplyTypeExists(Integer id) {
        if (supplyTypeMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public SupplyTypeDO getSupplyType(Integer id) {
        return supplyTypeMapper.selectById(id);
    }

    @Override
    public PageResult<SupplyTypeDO> getSupplyTypePage(SupplyTypePageReqVO pageReqVO) {
        return supplyTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SupplyTypeDO> getSupplyTypeList(SupplyTypePageReqVO pageReqVO) {
        return supplyTypeMapper.selectList(new MPJLambdaWrapperX<SupplyTypeDO>()
                .likeIfPresent(SupplyTypeDO::getTypeName,pageReqVO.getTypeName())
                .likeIfPresent(SupplyTypeDO::getFragranceTypes,pageReqVO.getFragranceType())
                .eqIfPresent(SupplyTypeDO::getVisible, pageReqVO.getVisible()));
    }

    @Override
    public List<SupplyTypeDO> selectTypeList() {
        // 查询可见的类型
        return supplyTypeMapper.selectList(new LambdaQueryWrapperX<SupplyTypeDO>()
                .eq(SupplyTypeDO::getVisible, SupplyTypeEnum.IS_VISIBLE.getCode())
                .orderByAsc(SupplyTypeDO::getSort)
                .orderByDesc(SupplyTypeDO::getCreateTime)
        );
    }

    @Override
    public List<SupplyTypeDO> appSelectTypeList() {
        return supplyTypeMapper.selectList(new LambdaQueryWrapperX<SupplyTypeDO>()
                .eq(SupplyTypeDO::getVisible, SupplyTypeEnum.IS_VISIBLE.getCode())
                .orderByAsc(SupplyTypeDO::getSort)
                .orderByDesc(SupplyTypeDO::getCreateTime)
        );
    }

    @Override
    public List<SupplyTypeDO> getSupplyTypeListByOdorType(Integer odorTypeValue) {
        List<DictDataRespDTO> odorType = dataApi.getDictDataList("odor_type");

        List<DictDataRespDTO> collect = odorType.stream().filter(item -> item.getValue().equals(String.valueOf(odorTypeValue)))
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(collect)){
            throw exception(DICT_NOT_EXISTS);
        }
        return supplyTypeMapper.selectList(new LambdaQueryWrapperX<SupplyTypeDO>()
                .likeIfPresent(SupplyTypeDO::getFragranceTypes,collect.get(0).getLabel())
                //查询可见的数据
                .eqIfPresent(SupplyTypeDO::getVisible,1)
                .orderByAsc(SupplyTypeDO::getSort)
                .orderByDesc(SupplyTypeDO::getCreateTime));

    }

}
